package cn.jhc.dao.impl;

import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.hibernate.Session;

import cn.jhc.bean.Student;
import cn.jhc.dao.StudentDao;
import cn.jhc.util.HibernateUtil;

public class StudentDaoImpl implements StudentDao {
	
	private static Logger LOG = Logger.getLogger(StudentDaoImpl.class.getName());
	
	@Override
	public void save(Object o) {
		if (o instanceof Student) {
			Session session = HibernateUtil.getSession();
			session.beginTransaction();
			session.save(o);
			session.getTransaction().commit();
			return;
		}
		LOG.info("save失败，请传入" + Student.class.getName() + "的对象");
	}

	@Override
	public void update(Object o) {
		if (o instanceof Student) {
			Session session = HibernateUtil.getSession();
			session.beginTransaction();
			session.update(o);
			session.getTransaction().commit();
			return;
		}
		LOG.info("update失败，请传入" + Student.class.getName() + "的对象");
	}

	@Override
	public void delete(Object o) {
		if (o instanceof Student) {
			Session session = HibernateUtil.getSession();
			session.beginTransaction();
			session.delete(o);
			session.getTransaction().commit();
			return;
		}
		LOG.info("delete失败，请传入" + Student.class.getName() + "的对象");
	}

	@Override
	public boolean verify(String username, String password) {
		String hql = "select count(stu.sno) from Student stu where stu.sno = ? and stu.spassword = ?";
		Long count = 0l;
		Session session = HibernateUtil.getSession();
		session.beginTransaction();
		Query query = session.createQuery(hql);
		query.setString(0, username);
		query.setString(1, password);
		count = (Long)query.uniqueResult();
		session.getTransaction().commit();
		LOG.info("count " + count);
		return count == 1;
	}

	@Override
	public boolean verify(Object o) {
		if (o instanceof Student) {
			Student student = (Student)o;
			return verify(student.getSno().toString(), student.getSpassword());
		}
		LOG.info("请传入" + Student.class.getName() + "的对象");
		return false;
	}

	@Override
	public boolean isThereUser(String username) {
		String hql = "select count(stu.sno) from Student stu where stu.sno = ?";
		Long count = 0l;
		Session session = HibernateUtil.getSession();
		session.beginTransaction();
		Query query = session.createQuery(hql);
		query.setString(0, username);
		count = (Long)query.uniqueResult();
		session.getTransaction().commit();
		return count != 0;
	}

	@Override
	public Student getStudent(String username) {
		
		List<Student> students = null;
		String hql = "from Student s where s.sno = ?";
		Session session = HibernateUtil.getSession();
		session.beginTransaction();
		Query query =session.createQuery(hql);
		query.setString(0, username);
		students = query.list();
		session.getTransaction().commit();
		if (students != null && students.size() >= 1) {
			return students.get(0);
		}
		return null;
	}


	@Override
	public Object get(int id) {
		Student student = null;
		Session session = HibernateUtil.getSession();
		session.beginTransaction();
		student = session.get(Student.class, id);
		session.getTransaction().commit();
		return student;
	}

	@Override
	public List list() {
		String hql = "from Student";
		List<Student> students = null;
		Session session = HibernateUtil.getSession();
		session.beginTransaction();
		Query query = session.createQuery(hql);
		students = query.list();
		session.getTransaction().commit();
		return students;
	}

	@Override
	public List list(int start, int count) {
		String hql = "from Student";
		List<Student> students = null;
		Session session = HibernateUtil.getSession();
		session.beginTransaction();
		Query query = session.createQuery(hql);
		query.setFirstResult(start);
		query.setMaxResults(count + start);
		students = query.list();
		session.getTransaction().commit();
		return students;
	}

	@Override
	public long getCount() {
		String hql = "select count(s.sno) from Student s";
		Long count = 0l;
		Session session = HibernateUtil.getSession();
		session.beginTransaction();
		Query query = session.createQuery(hql);
		count = (Long)query.uniqueResult();
		session.getTransaction().commit();
		return count;
	}

	@Override
	public List<Student> list(String studentName, String no, int start, int count) {
		String hql = "from Student s where s.sname like ? and s.sno like ?";
		
		List<Student> students = null;
		no = no == null ? "" : no;
		studentName = studentName == null ? "" : studentName;
		
		Session session = HibernateUtil.getSession();
		session.beginTransaction();
		Query query = session.createQuery(hql);
		query.setString(0, "%" + studentName + "%");
		query.setString(1, "%" +  no + "%");
		query.setFirstResult(start);
		query.setMaxResults(count + start);
		students = query.list();
		session.getTransaction().commit();
		
		return students;
	}
	
	@Override
	public List<Student> list(String studentName, int start, int count) {
		return list(studentName, null, start, count);
	}

}
